package com.jdd.modules.configcartype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.CacheConstant;
import com.jdd.modules.configcartype.entity.ConfigCarType;
import com.jdd.modules.configcartype.mapper.ConfigCarTypeMapper;
import com.jdd.modules.configcartype.service.IConfigCarTypeService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 车辆类型管理
 * @Author: jdd
 * @Date: 2021-02-18
 * @Version: V1.0
 */
@Service
public class ConfigCarTypeServiceImpl extends ServiceImpl<ConfigCarTypeMapper, ConfigCarType> implements IConfigCarTypeService {

    @Resource
    ConfigCarTypeMapper configCarTypeMapper;

    /**
     * 获取车场所有的车辆类型
     *
     * @return 车辆类型列表
     */
    @Override
    public List<ConfigCarType> getCarTypeList() {
        LambdaQueryWrapper<ConfigCarType> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }

    /**
     * 根据id获取车辆类型信息
     *
     * @param id 车辆类型id
     * @return 车辆类型
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, key = "#id")
    public ConfigCarType findCarTypeById(String id) {
        return this.getById(id);
    }

    /**
     * 获取车场所有的月租车类型
     *
     * @return 月租车辆类型列表
     */
    @Override
    public List<ConfigCarType> findMonthlyCarTypeList() {
        LambdaQueryWrapper<ConfigCarType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigCarType::getCarType, "1");
        return this.list(queryWrapper);
    }

    /**
     * 清空表数据
     *
     * @return true 清空成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID,allEntries = true)
    })
    public boolean removeAllInfo() {
        try {
            configCarTypeMapper.removeAllInfo();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 获取车场所有的储值车类型
     *
     * @return 储值车辆类型列表
     */
    @Override
    public List<ConfigCarType> findStoreCarTypeList() {
        LambdaQueryWrapper<com.jdd.modules.configcartype.entity.ConfigCarType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(com.jdd.modules.configcartype.entity.ConfigCarType::getCarType, "2");
        return this.list(queryWrapper);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)
    })

    /**
     * 根据车场编号删除车辆类型数据
     *
     * @return 删除结果
     */
    public Boolean removeByParkCode() {
        LambdaQueryWrapper<ConfigCarType> queryWrapper = new LambdaQueryWrapper<>();
        return this.remove(queryWrapper);
    }

    /**
     * 保存车辆类型实体
     *
     * @param configCarType 车辆类型实体
     * @return 保存结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)
    })
    public boolean saveEntity(ConfigCarType configCarType) {
        if (configCarTypeMapper.insert(configCarType) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新车辆类型实体
     *
     * @param configCarType 车辆类型实体
     * @return 更新结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)
    })
    public boolean updateEntity(ConfigCarType configCarType) {
        if (configCarTypeMapper.updateById(configCarType) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 删除指定id的车辆类型实体
     *
     * @param id 车辆类型id
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)})
    public boolean deleteEntityById(String id) {
        if (configCarTypeMapper.deleteById(id) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据id列表批量删除指定id的车辆类型实体
     *
     * @param ids 车辆类型id列表
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)})
    public boolean deleteEntityByIds(List<String> ids) {
        if (configCarTypeMapper.deleteBatchIds(ids) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 根据车辆类型列表保存
     *
     * @param list 车辆类型列表
     * @return 保存结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CONFIG_CAR_TYPE_BY_ID, allEntries = true)})
    public Boolean saveConfigCarTypeByList(List<ConfigCarType> list) {
        return this.saveBatch(list);
    }
}
